require( './style.less' );
define( [
  'require',
  'constants',
  'behavior'
], function( require ) {
  'use strict';

  var behavior = require( 'behavior' );
  var constants = require( 'constants' );
  var returnParam = {};

  var me = returnParam.$selector = $( '.search_area.search_material' );

  var address_array = {
    art: {
      name: behavior.searchArt
    },
    image: {
      name: behavior.searchImg
    },
    gzh: {
      name: behavior.searchGzh
    },
    tpc: {
      name: behavior.searchTpc
    }
  };

  /**
   * [addClassByIndex description]
   * @param {[type]} index [description]
   */
  returnParam.addClassByIndex = function( index ) {
    index = index || 0;
    $( '.search_type .search_item', me ).eq( index ).addClass( 'selected' ).siblings().removeClass( 'selected' );
    this.currentIndex = index;
  };

  /**
   * [setInputVal description]
   * @param {[type]} val          [description]
   * @param {[type]} triggerEvent [description]
   */
  returnParam.setInputVal = function( val, isNotTriggerEvent ) {
    $( '.search_ipt', me ).val( val );
    if ( !isNotTriggerEvent ) {
      $( '.search_link', me ).trigger( 'click' );
    }
  };

  function openSearch() {
    var head_search_type = $( this ).attr( 'kw' );
    var search_ipt = $.trim( $( '.search_ipt', me ).val() );
    var head_search_key = encodeURI( search_ipt );
    var searchObj = address_array[ head_search_type ];
    var search_address = $( this ).attr( 'href' );
    var navLink = '';
    if ( search_ipt === '' ) {
      if ( head_search_type === 'tpc' ) {
        navLink = search_address;
      } else {
        navLink = search_address + '?from_nav=1';
      }
    } else {
      if ( head_search_type === 'tpc' ) {
        navLink = search_address + '/search?keyword=' + head_search_key;
      } else {
        navLink = search_address + '?search_key=' + head_search_key;
      }
    }

    behavior.addRecord( {
      category: searchObj.name,
      action: search_ipt,
      label: constants.userLoginBehavior
    } );
    location.href = navLink;
  }

  me.on( 'click', '.search_item a', function( e ) {
    e.preventDefault();
    var searchParam = $.trim( $( '.search_ipt', me ).val() );
    if ( !searchParam && searchParam !== '0' ) {
      $( this ).parent().addClass( 'selected' ).siblings().removeClass( 'selected' );
    } else {
      openSearch.call( this );
    }
  } ).on( 'click', '.search_link', function() { // 搜索按钮点击
    if ( $( '.search_item.selected', me ).index() !== returnParam.currentIndex ) {
      openSearch.call( $( '.search_type li.selected a', me ) );
    } else {
      $.isFunction( returnParam.onSearch ) && returnParam.onSearch.call( me, $.trim( $( '.search_ipt', me ).val() ) );
    }
  } ).on( {
    keypress: function( event ) {
      if ( event.keyCode == 13 ) {
        $( '.search_link', me ).trigger( 'click' );
      }
    },
    focus: function() {
      me.addClass( 'active' );
    },
    blur: function() {
      me.removeClass( 'active' );
    }
  }, '.search_ipt' );


  return returnParam;

} );
